{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ipywidgets.widgets as iwidgets\n",
    "from IPython.display import display, clear_output\n",
    "import random\n",
    "\n",
    "output = iwidgets.Output()\n",
    "display(output)\n",
    "out = \"None\"\n",
    "def render(*args):\n",
    "    r = random.random()\n",
    "\n",
    "    # nonlocal out\n",
    "    with output:\n",
    "        clear_output()\n",
    "        print(r, \"Outside\")\n",
    "        out = iwidgets.Output()\n",
    "        with out:\n",
    "            print(r, \"Inside\")\n",
    "        display(out)\n",
    "\n",
    "render()\n",
    "\n",
    "button = iwidgets.Button(description=\"Redraw\")\n",
    "button.on_click(render)\n",
    "display(button)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import ipywidgets.widgets as iwidgets\n",
    "from IPython.display import display, clear_output\n",
    "import random\n",
    "\n",
    "output1 = iwidgets.Output()\n",
    "display(output1)\n",
    "output2 = iwidgets.Output()\n",
    "\n",
    "def render(*args):\n",
    "    r = random.random()\n",
    "\n",
    "    with output1:\n",
    "        clear_output()\n",
    "        print(r, \"Outside\")\n",
    "        with output2:\n",
    "            clear_output()\n",
    "            print(r, \"Inside\")\n",
    "        display(output2)\n",
    "\n",
    "render()\n",
    "\n",
    "button = iwidgets.Button(description=\"Redraw\")\n",
    "button.on_click(render)\n",
    "display(button)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with output1:\n",
    "\tprint('Hello')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with output2:\n",
    "\tprint('World')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with output2:\n",
    "\tclear_output()\n",
    "\tprint('Foo')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with output1:\n",
    "\tclear_output()\n",
    "\tprint('Bar')"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
